package com.component;

import com.Listener.ActionDoneListener;
import com.bean.Product;
import com.dao.ProductDao;
import com.util.ScreenUtils;
import com.util.SelectProduct;
import com.util.SelectProductCategory;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddProductDialog extends JDialog {
    final int WIDTH = 500;
    final int HEIGHT = 400;

    private ActionDoneListener listener;

    public AddProductDialog(JFrame jf, String title, boolean isModel,ActionDoneListener listener,int FarmerID){
        super(jf,title,isModel);
        this.listener  = listener;
        //组装视图
        this.setBounds((ScreenUtils.getScreenWidth()-WIDTH)/2,(ScreenUtils.getScreenHeight()-HEIGHT)/2,WIDTH,HEIGHT);

        Box vBox = Box.createVerticalBox();

        //组装农产品编号
        Box IDBox = Box.createHorizontalBox();
        JLabel IDLabel = new JLabel("农产品编号：");
        JTextField IDField = new JTextField(15);

        IDBox.add(IDLabel);
        IDBox.add(Box.createHorizontalStrut(15));
        IDBox.add(IDField);

        //组装农产品类型
        Box typeBox = Box.createHorizontalBox();
        JLabel typeLabel = new JLabel("农产品类型：");
        JTextField typeField = new JTextField(15);

        typeBox.add(typeLabel);
        typeBox.add(Box.createHorizontalStrut(15));
        typeBox.add(typeField);

        //组装农产品名称
        Box ProductNameBox = Box.createHorizontalBox();
        JLabel ProductNameLabel = new JLabel("农产品名称：");
        JTextField ProductNameField = new JTextField(15);

        ProductNameBox.add(ProductNameLabel);
        ProductNameBox.add(Box.createHorizontalStrut(15));
        ProductNameBox.add(ProductNameField);

        //组装农产品价格
        Box ProductPriceBox = Box.createHorizontalBox();
        JLabel ProductPriceLabel = new JLabel("农产品价格：");
        JTextField ProductPriceField = new JTextField(15);

        ProductPriceBox.add(ProductPriceLabel);
        ProductPriceBox.add(Box.createHorizontalStrut(15));
        ProductPriceBox.add(ProductPriceField);

        //组装图片路径
        Box pathBox = Box.createHorizontalBox();
        JLabel pathLabel = new JLabel("图 片 路 径 ：");
        JTextField pathField = new JTextField(15);

        pathBox.add(pathLabel);
        pathBox.add(Box.createHorizontalStrut(15));
        pathBox.add(pathField);

        //组装描述信息
        Box descriptionBox = Box.createHorizontalBox();
        JLabel descriptionLabel = new JLabel("描 述 信 息 ：");
        JTextArea descriptionArea = new JTextArea(3,15);

        descriptionBox.add(descriptionLabel);
        descriptionBox.add(Box.createHorizontalStrut(15));
        descriptionBox.add(descriptionArea);

        //组装按钮
        Box btnBox = Box.createHorizontalBox();
        JButton addBtn = new JButton("添加");
        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取用户的录入
                int ProductID = Integer.parseInt(IDField.getText().trim());
                int ProductCategoryID = new SelectProductCategory().getProductCategoryID(typeField.getText().trim());
                String ProductName = ProductNameField.getText().trim();
                Double Price = Double.valueOf(ProductPriceField.getText().trim());
                String ImagePath = pathField.getText().trim();
                String description = descriptionArea.getText().trim();

                if(IDField.getText().trim().length()!=5){
                    JOptionPane.showMessageDialog(jf, "农产品编号要为五位数字！");
                }
                else if(new SelectProduct().exitProductID(ProductID)){
                    JOptionPane.showMessageDialog(jf,"已存在此农产品编号！");
                }
                else if(ProductCategoryID==0){
                    JOptionPane.showMessageDialog(jf,"没有此农产品类型！");
                }
                else{
                    //将数据添加进数据库
                    Product product = new Product(ProductID,ProductCategoryID,FarmerID,ProductName,Price,ImagePath,description);
                    new ProductDao().insertProduct(product);
                    JOptionPane.showMessageDialog(jf, "添加成功!");
                    listener.done(null);
                    dispose(); // 关闭添加窗口
                }
            }
        });


        btnBox.add(addBtn);

        vBox.add(Box.createVerticalStrut(20));
        vBox.add(IDBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(typeBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(ProductNameBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(ProductPriceBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(pathBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(descriptionBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(btnBox);

        //为了左右有间距，在vBox外层封装一个水平的Box，添加间隔
        Box hBox = Box.createHorizontalBox();
        hBox.add(Box.createHorizontalStrut(50));
        hBox.add(vBox);
        hBox.add(Box.createHorizontalStrut(50));

        this.add(hBox);
    }
}
